// pages/blogpost/body.js
var WxParse = require('../../vendor/wxParse/wxParse.js')

Page({
    data: {
        article: '',
        comments: [],
        postId: 0,
        blogApp: '',
        commentInput: '',
        pageIndex: 1,
        pageSize: 10,
    },
    onLoad: function (options) {
        this.loadArticle(options);
        this.loadComments(options);
    },

    loadArticle: function (options) {
        // 页面初始化 options为页面跳转所带来的参数
        var that = this;
        wx.request({
            url: 'https://api.cnblogs.com/api/blogposts/' + options.postId + '/body',
            method: 'GET',
            header: {
                "Content-Type": "application/json; charset=utf-8",
                "Authorization": "Bearer " + getApp().accessToken
            },
            success: function (res) {
                if (res) {
                    that.setData({
                        article: res.data,
                        postId: options.postId,
                        blogApp: options.blogApp
                    })

                    WxParse.wxParse('article', 'html', that.data.article, that, 5);
                }

            },
            fail: function () {
                // fail
            },
            complete: function () {
                // complete
            }
        })
    },

    loadComments: function (options) {
        var that = this;
        wx.request({
            url: 'https://api.cnblogs.com/api/blogs/' + options.blogApp + '/posts/' +
                    options.postId + '/comments?pageIndex=' + this.data.pageIndex + '&pageSize=' + this.data.pageSize,
            method: 'GET',
            header: {
                "Content-Type": "application/json; charset=utf-8",
                "Authorization": "Bearer " + getApp().accessToken
            },
            success: function (res) {
                if (res) {
                    that.setData({
                        comments: res.data
                    })
                }
            },
            fail: function () {
                // fail
            },
            complete: function () {
                // complete
            }
        })
    },

    addComment: function (options) {
        console.log(options);
        wx.request({
            url: 'https://api.cnblogs.com/api/blogs/' + this.data.blogApp + '/posts/' + this.data.postId + '/comments',
            method: 'POST',
            data: {
                "": this.data.commentInput
            },
            header: {
                "Content-Type": "application/x-www-form-urlencoded",
                'Authorization': 'Bearer ' + getApp().accessToken,
            },
            success: function (res) {
                console.log(res)
            }

        });
    },
    listenCommontInput: function (event) {
        this.data.commentInput = event.detail.value;
    },
    onReady: function () {
        // 页面渲染完成
    },
    onShow: function () {
        // 页面显示
    },
    onHide: function () {
        // 页面隐藏
    },
    onUnload: function () {
        // 页面关闭
    }
})